/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package apus.action.user;

import apus.action.Action;
import apus.action.ActionResult;
import apus.authorisation.UserAuthorisation;
import apus.entity.User;
import apus.persistence.UserDAO;
import apus.services.ServiceLocator;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 21vek
 */
public class UserDeleteAction implements Action {

    @Override
    public String getName() {
        return "user.delete";
    }

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {
        if (request.getParameter("id") != null) {
            int id = Integer.parseInt(request.getParameter("id"));
            UserDAO udao = ServiceLocator.getService(UserDAO.class);
            String userLogin = request.getParameter("login");
            String currentAdminLogin = ((UserAuthorisation)request.getSession().getAttribute("userAuth")).getLogin();
            if (!userLogin.equals(currentAdminLogin)) {
                udao.delete(new User(id));
                request.getSession().setAttribute("win", "User was removed");
            } else {
                request.getSession().setAttribute("fail", "You can't remove yourself :)");
            }
        } else {
            request.getSession().setAttribute("fail", "User wasn't removed");
        }
        return ActionResult.redirect("administration");
    }
}
